package common

import "sync"

type ChatMessage struct {
	Role       string `json:"role"`
	Content    string `json:"content"`
	ToolCalls  []Tool `json:"tool_calls,omitempty"`
	ToolCallId string `json:"tool_call_id,omitempty"`
}

// HistoryManager 新增历史管理结构体
type HistoryManager struct {
	messages  []ChatMessage
	maxRounds int
	mu        sync.Mutex
}

func NewHistoryManager() HistoryManager {
	return HistoryManager{
		messages:  make([]ChatMessage, 0),
		maxRounds: DefaultRounds,
		mu:        sync.Mutex{},
	}
}

// AddDialogue 添加历史数据
func (h *HistoryManager) AddDialogue(msg ChatMessage) {
	h.mu.Lock()
	defer h.mu.Unlock()
	if h.maxRounds > 0 && len(h.messages) >= 2*h.maxRounds {
		h.messages = h.messages[2:]
	}
	h.messages = append(h.messages, msg)
}

// GetDialogue 获取历史数据
func (h *HistoryManager) GetDialogue() []ChatMessage {
	h.mu.Lock()
	defer h.mu.Unlock()
	return h.messages
}

// SetMaxRounds 设置最大对话轮数
func (h *HistoryManager) SetMaxRounds(rounds int) {
	h.mu.Lock()
	defer h.mu.Unlock()
	h.maxRounds = rounds
}

func (h *HistoryManager) Clear() {
	h.mu.Lock()
	defer h.mu.Unlock()
	h.messages = make([]ChatMessage, 0)
}
